function hook_sort() {

    var Collections = Java.use('java.util.Collections');
    // Hook sort() 方法
    Collections.sort.overload("java.util.List").implementation = function (list) {
        console.log('Hooked Collections.sort()');
        console.log('List: ' + list.toString());
        // 可在此处对参数进行修改或记录
        // 使用 Java.cast 进行类型转换 将list转换成ArrayList类型
        var res = Java.cast(list, Java.use("java.util.ArrayList"))
        console.log('List list-->', res)
        // 调用原始的 sort() 方法
        // 从这里看堆栈 找源码出现的位置
        return this.sort(list);
    };


    Collections.sort.overload("java.util.List", "java.util.Comparator").implementation = function (a, b) {
        console.log('Hooked Collections.sort()');
        var res = Java.cast(a, Java.use("java.util.ArrayList"))
        console.log('Comparator list-->', res)
        return this.sort(a, b);
    };
}

function hook_json() {
    console.log("===============hook josn ================")
    var JSONObject = Java.use('org.json.JSONObject');
// Hook JSONObject.put() 方法
    JSONObject.put.overload('java.lang.String', 'java.lang.Object').implementation = function (key, value) {
        // console.log('Hooked JSONObject.put()');
        // console.log('Key: ' + key.toString());
        // console.log('Value: ' + value.toString());
        // 可在此处对参数进行修改或记录
        // 调用原始的put()方法
        if (key == "Encrypt") {
            // showStacks()
        }
        return this.put(key, value);
    };

// Hook JSONObject.getString() 方法  从json里面取值
    JSONObject.getString.overload('java.lang.String').implementation = function (key) {
        console.log('Hooked JSONObject.getString()');
        console.log('Key: ' + key.toString());
        // 调用原始的getString()方法
        //     showStacks()
        var result = this.getString(key);
        // 可在此处对返回值进行修改或记录
        return result;
    };
}

function hook_String() {
    var StringClass = Java.use('java.lang.String');
    // Hook String 类的构造函数
    StringClass.getBytes.overload().implementation = function () {
        console.log('Original Value');
        // 可在此处修改传入的字符串参数
        var res = this.getBytes();
        var newString = StringClass.$new(res)
        // 输出修改后的值
        console.log('Modified Value: ' + newString);
        return res;
    };

    // Hook String 类的静态方法
    StringClass.getBytes.overload('java.lang.String').implementation = function (obj) {
        console.log('Hooked String.valueOf()');
        // 可在此处修改传入的对象参数
        var res = this.getBytes(obj);
        var newString = StringClass.$new(res, obj)
        // 输出修改后的结果
        console.log('getBytes: ' + newString)
        return res
    }


}

function hook_StringBuilder() {

    var stringBuilderClass = Java.use("java.lang.StringBuilder");
    stringBuilderClass.toString.implementation = function () {
        var res = this.toString.apply(this, arguments)
        // if (res.includes("_sig")){
        //     showStacks()
        // }

        return res
    }

    let ApiSignatureHelper = Java.use("com.douban.frodo.network.ApiSignatureHelper");
    ApiSignatureHelper["a"].overload('java.lang.String', 'java.lang.String', 'java.lang.String').implementation = function (str, str2, str3) {
        console.log(`ApiSignatureHelper.m28657a is called: str=${str}, str2=${str2}, str3=${str3}`);
        let result = this["a"](str, str2, str3);
        console.log(`ApiSignatureHelper.m28657a result=${result}`);
        return result;
    };

}

function hook_map() {
    var hashMap = Java.use("java.util.HashMap");
    hashMap.put.implementation = function (a, b) {
        console.log("hook map ->> key:", a, "value:", b);
        // if (a.equals("username")) {
        //     showStacks();
        // }
        return this.put(a, b);
    }

}

function hook_text() {

    var editText = Java.use("android.widget.EditText");
    editText.getText.overload().implementation = function () {
        var result = this.getText();
        result = Java.cast(result, Java.use("java.lang.CharSequence"));
        console.log("editText.getText: ", result.toString());
        return result;
    }

}

function hook_list() {
    var arrayList = Java.use("java.util.ArrayList");
    arrayList.add.overload('java.lang.Object').implementation = function (a) {
        // if (a.equals("username=12555")) {
        //     console.log("arrayList.add: ", a);
        // }
        console.log("arrayList.add: ", a);
        return this.add(a);
    }
    arrayList.add.overload('int', 'java.lang.Object').implementation = function (a, b) {
        console.log("arrayList.add: ", a, b);
        return this.add(a, b);
    }

}

function hook_lib() {
    Java.perform(function () {
        let Systemclazz = Java.use("java.lang.System");
        Systemclazz.loadLibrary.implementation = function () {  // loadLibrary、load、mapLibraryName等，都hook看看
            console.log('System.loadLibrary is called');
            console.log('library name:', arguments[0]);
            var retval = Systemclazz.loadLibrary.apply(this, arguments)

            //打印堆栈
            console.log(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Throwable").$new()));
            return retval
        };

    });
}

function hook_so(){
    var artModule = Process.findModuleByName("libart.so");
    var symbols = artModule.enumerateSymbols();
    var newStringUTF = null;
    for (let i = 0; i < symbols.length; i++) {
        let symbol = symbols[i];
        if(symbol.name.indexOf("NewStringUTF") != -1 && symbol.name.indexOf("secur") == -1){
            console.log(symbol.name);
            newStringUTF = symbol.address;
        }

    }

    // Interceptor.attach(newStringUTF, {
    //     onEnter : function(args){
    //         if(args[1].readCString() && args[1].readCString().indexOf("*")!= -1 && args[1].readCString().indexOf("B")!= -1){  // 设置密文过滤规则
    //             console.log("\x1B[33m[加密字符串]:\x1B[0m",args[1].readCString());
    //             console.log("\x1B[33m[调用栈]:\x1B[0m\n",Thread.backtrace(this.context, Backtracer.ACCURATE).map(DebugSymbol.fromAddress).join('\n') + '\n');
    //         }
    //     },onLeave : function(retval){
    //
    //     }
    // })
}

function main() {
    Java.perform(function () {
        hook_so()
    })
}

setImmediate(main)